#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "welcome.h"
#include "work.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //页面切换
    welcome_widget = new Welcome(this);
    work_widget = new Work(this);
    ui->stackedWidget->addWidget(welcome_widget);
    ui->stackedWidget->addWidget(work_widget);
    ui->stackedWidget->setCurrentIndex(0);
    //右键菜单
    _menu = new QMenu();
    _menu->addAction(ui->act_insert_pic);
    _menu->addAction(ui->act_Export_pic);
    //打开图片
    connect(this,&MainWindow::SignImagePath,work_widget,&Work::SlotImagePath);
    connect(this,&MainWindow::SignSaveImagePath,work_widget,&Work::SlotSaveImagePath);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_toolButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}
void MainWindow::on_toolButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

//导入图片
void MainWindow::on_act_insert_pic_triggered()
{
    //打开图片文件，选择图片
    _filename=QFileDialog::getOpenFileName(this,tr("Open Image"),QDir::homePath(),tr("(*.jpg)\n(*.bmp)\n(*.png)"));
    emit SignImagePath(_filename);
}

//保存图片
void MainWindow::on_act_Export_pic_triggered()
{
    if(_filename.isEmpty()){
        emit work_widget->SignImageIsNull();
        return;
    }
    QString currentPath = QCoreApplication::applicationDirPath();
    QString path = QFileDialog::getSaveFileName(nullptr, QStringLiteral("保存文件"), currentPath, QStringLiteral("(*.jpg)\n(*.bmp)\n(*.png)"));
    emit this->SignSaveImagePath(path);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()== Qt::RightButton){
        _menu->exec(QCursor::pos());
    }
    QMainWindow::mousePressEvent(event);
}
